﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WiTeach.ClassManagement.DAL;
using WiTeach.ClassManagement.Model;
using WiTeach.ClassManagement.ViewModel;
using WiTeach.CourseManagement.DAL;
using WiTeach.CourseManagement.Model;

namespace WiTeach.CourseManagement.View
{
    /// <summary>
    /// CourseList.xaml 的交互逻辑
    /// </summary>
    public partial class CourseList : Page
    {

        private static int pageNow = 0;
        private static int pageSize = 6;
        public int LessonID;
        public CourseList()
        {
            InitializeComponent();
            // 修改页面背景主题
            ImageBrush brush = new ImageBrush();
            brush.Stretch = Stretch.Fill;
            brush.ImageSource = new BitmapImage(new Uri(App.backgroundImage, UriKind.RelativeOrAbsolute));
            this.Background = brush;
        }

        public CourseList(int Lesson_ID)
        {
            InitializeComponent();
            // 修改页面背景主题
            ImageBrush brush = new ImageBrush();
            brush.Stretch = Stretch.Fill;
            brush.ImageSource = new BitmapImage(new Uri(App.backgroundImage, UriKind.RelativeOrAbsolute));
            this.Background = brush;

            LessonID = Lesson_ID;
            // 获取标题
            
            string title = CourseDAL.getCourseNameByID(Lesson_ID);

            this.courseListTitle.Content = title;

            // 分页
            int totalCount = ClassDAL.getCountByID(Lesson_ID);
            int pageCount = (totalCount + pageSize - 1) / pageSize;
            if (pageCount >= 2)
            {
                this.prePageArrow.Visibility = System.Windows.Visibility.Visible;
                this.nextPageArrow.Visibility = System.Windows.Visibility.Visible;
            }

            initCourseList(ClassDAL.getCurrentPagingList(LessonID, 0, pageSize, pageCount));



  
        }

        #region 课时列表分页

        private void prePage(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            if (!btn.IsVisible)
                return;
            int totalCount = ClassDAL.getCountByID(LessonID);
            int pageCount = (totalCount + pageSize - 1) / pageSize;
            --pageNow;
            setLessonButtonVisibility();
            initCourseList(ClassDAL.getCurrentPagingList(LessonID, pageNow, pageSize, pageCount));
        }

        private void nextPage(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            if (!btn.IsVisible)
                return;
            int totalCount = ClassDAL.getCountByID(LessonID);
            int pageCount = (totalCount + pageSize - 1) / pageSize;
            ++pageNow;
            setLessonButtonVisibility();
            initCourseList(ClassDAL.getCurrentPagingList(LessonID, pageNow, pageSize, pageCount));
        }

        #endregion

        #region 课时按钮操作相关内容
        /// <summary>
        /// 遍历数据集合内容，控制控件隐藏和显示
        /// </summary>
        /// <param name="classList"> 数据集合</param>
        public void initCourseList(List<Class> classList)
        {
            if (classList == null)
                return;
            string btnName = "lessonBtn_";
            for (int i = 0; i < classList.Count; i++)
            {
                    // 拼接字符串控制属性显示
                btnName += (i+1);
                switch (btnName.ToString())
                {
                    case "lessonBtn_1":
                        this.lessonBtn_1.DataContext = classList[i];
                        this.lessonBtn_1.Visibility = System.Windows.Visibility.Visible;
                        break;
                    case "lessonBtn_2":
                        this.lessonBtn_2.DataContext = classList[i];
                        this.lessonBtn_2.Visibility = System.Windows.Visibility.Visible;
                        break;
                    case "lessonBtn_3":
                        this.lessonBtn_3.DataContext = classList[i];
                        this.lessonBtn_3.Visibility = System.Windows.Visibility.Visible;
                        break;
                    case "lessonBtn_4":
                        this.lessonBtn_4.DataContext = classList[i];
                        this.lessonBtn_4.Visibility = System.Windows.Visibility.Visible;
                        break;
                    case "lessonBtn_5":
                        this.lessonBtn_5.DataContext = classList[i];
                        this.lessonBtn_5.Visibility = System.Windows.Visibility.Visible;
                        break;
                    case "lessonBtn_6":
                        this.lessonBtn_6.DataContext = classList[i];
                        this.lessonBtn_6.Visibility = System.Windows.Visibility.Visible;
                        break;

                }
                
                btnName = "lessonBtn_";
               
            }
        }


        /// <summary>
        /// 隐藏按钮状态
        /// </summary>
        public void setLessonButtonVisibility()
        {
            this.lessonBtn_1.Visibility = System.Windows.Visibility.Hidden;
            this.lessonBtn_2.Visibility = System.Windows.Visibility.Hidden;
            this.lessonBtn_3.Visibility = System.Windows.Visibility.Hidden;
            this.lessonBtn_4.Visibility = System.Windows.Visibility.Hidden;
            this.lessonBtn_5.Visibility = System.Windows.Visibility.Hidden;
            this.lessonBtn_6.Visibility = System.Windows.Visibility.Hidden;
 
        }


        /// <summary>
        /// 课时点击事件，跳转至课时详情页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShowLessonDetail(object sender, RoutedEventArgs e)
        {
            Button lesson = (Button)sender;
            int ClassHour_ID=0;
            //string strTag = lesson.Tag.ToString();
            //if (strTag.Length >= 2)
            //{
            //    //string subID = strTag.Substring(1, strTag.Length);
            //    string subID = strTag.Remove(0, 1);
            //    ClassHour_ID = Convert.ToInt32(subID);
            //}
            //else
            //{
                ClassHour_ID = (int)lesson.Tag;
            //}
            //ClassHour_ID = (int)lesson.Tag;
            string ClassHour_Name = lesson.Content.ToString();

            ClassIndex lessonIndexPage = new ClassIndex(ClassHour_ID, ClassHour_Name, LessonID);
            this.NavigationService.Navigate(lessonIndexPage);
        }

        #endregion

        #region 页面导航

        // 返回主目录
        private void backHome(object sender, RoutedEventArgs e)
        {
            CourseIndex courseIndex = new CourseIndex();
            this.NavigationService.Navigate(courseIndex);
        }

        // 返回上级目录
        private void backPreLevel(object sender, RoutedEventArgs e)
        {
            CourseIndex courseIndex = new CourseIndex();
            this.NavigationService.Navigate(courseIndex);
        }

        #endregion

      

        private void ShowLessonDetail2(object sender, RoutedEventArgs e)
        {
            //Button lesson = (Button)sender;
            //int ClassHour_ID = (int)lesson.Tag;
            //string ClassHour_Name = lesson.Content.ToString();
            ClassIndex lessonIndexPage = new ClassIndex();
            this.NavigationService.Navigate(lessonIndexPage);
        }
        
        private void courselist_backmain_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 66;
            image.Width = 104;
        }
         private void courselist_backmain_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 60;
            image.Width = 98;
        }
         private void courselist_back_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 66;
            image.Width = 104;
        }
         private void courselist_back_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 60;
            image.Width = 98;
        }

         private void courselist_choice_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
         {
             Image image = sender as Image;
             image.Height = 124;
             image.Width = 256;
         }

         private void courselist_choice_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
         {
             Image image = sender as Image;
             image.Height = 120;
             image.Width = 250;
         }

         private void courselist_pagearrow_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
         {
             Image image = sender as Image;
             image.Height = 61;
             image.Width = 34;
         }

         private void courselist_pagearrow_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
         {
             Image image = sender as Image;
             image.Height = 52;
             image.Width = 25;
         }
 
    }
}
